package com.hghivln.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hghivln.constants.Constants;
import com.hghivln.mapper.ProviderMapper;
import com.hghivln.pojo.domain.Provider;
import com.hghivln.pojo.dto.ProviderDto;
import com.hghivln.pojo.vo.AjaxResult;
import com.hghivln.service.ProviderService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

@Service
public class ProviderServiceImpl implements ProviderService{
    @Autowired
    private ProviderMapper providerMapper;

    @Override
    public AjaxResult listProviderForPage(ProviderDto providerDto) {
        Page<Provider> page = new Page<>(providerDto.getPageNum(), providerDto.getPageSize());
        QueryWrapper<Provider> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(providerDto.getProviderName()),Provider.COL_PROVIDER_NAME,providerDto.getProviderName());
        qw.like(StringUtils.isNotBlank(providerDto.getContactName()),Provider.COL_CONTACT_NAME,providerDto.getContactName());
        qw.like(StringUtils.isNotBlank(providerDto.getContactTel()), Provider.COL_CONTACT_TEL, new Consumer<QueryWrapper<Provider>>() {
            @Override
            public void accept(QueryWrapper<Provider> provider) {
                provider.like(Provider.COL_CONTACT_TEL,providerDto.getContactTel())
                        .or().like(Provider.COL_CONTACT_MOBILE,providerDto.getContactMobile());
            }
        });
        qw.eq(StringUtils.isNotBlank(providerDto.getStatus()),Provider.COL_STATUS,providerDto.getStatus());
        qw.eq(Provider.COL_DEL_FLAG, Constants.DEL_FALSE);
        qw.orderByAsc(Provider.COL_CREATE_TIME);
        providerMapper.selectPage(page, qw);
        return AjaxResult.success("查询成功", page.getRecords(), page.getTotal());
    }

    @Override
    public AjaxResult addProvider(ProviderDto providerDto) {
        Provider provider = new Provider();
        BeanUtils.copyProperties(providerDto, provider);
        provider.setCreateBy(providerDto.getSimpleUser().getUserName());
        provider.setCreateTime(DateUtil.date());
        return AjaxResult.success(providerMapper.insert(provider));
    }

    @Override
    public AjaxResult updateProvider(ProviderDto providerDto) {
        Provider provider = new Provider();
        BeanUtils.copyProperties(providerDto, provider);
        provider.setUpdateBy(providerDto.getSimpleUser().getUserName());
        return AjaxResult.success(providerMapper.updateById(provider));
    }

    @Override
    public AjaxResult deleteProviderByIds(Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        if (idList.size() > 0) {
            return AjaxResult.success(providerMapper.deleteProviderByIds(idList));
        }
        return AjaxResult.fail("删除失败");
    }

    @Override
    public AjaxResult selectProviderById(Long id) {
        QueryWrapper<Provider> qw = new QueryWrapper<>();
        qw.eq(Provider.COL_DEL_FLAG, Constants.DEL_FALSE);
        qw.eq(Provider.COL_PROVIDER_ID, id);
        return AjaxResult.success(providerMapper.selectOne(qw));
    }

    @Override
    public AjaxResult selectAllProvider() {
        QueryWrapper<Provider> qw = new QueryWrapper<>();
        qw.eq(Provider.COL_DEL_FLAG, Constants.DEL_FALSE);
        qw.eq(Provider.COL_STATUS, Constants.STATUS_TRUE);
        return AjaxResult.success(providerMapper.selectList(qw));
    }

}
